package com.jeesite.modules.digitalize.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.digitalize.config.Constants;
import com.jeesite.modules.digitalize.dao.DigitalizeComponentNoteDao;
import com.jeesite.modules.digitalize.entity.DigitalizeComponentNote;
import com.jeesite.modules.digitalize.entity.dto.digitalizeComponentNote.DigitalizeComponentNoteDto;
import com.jeesite.modules.digitalize.entity.dto.digitalizeComponentNote.DigitalizeComponentNoteEditDto;
import com.jeesite.modules.digitalize.entity.dto.digitalizeComponentNote.DigitalizeComponentNoteQueryDto;
import com.jeesite.modules.digitalize.entity.vo.OptionSelectVo;
import com.jeesite.modules.digitalize.entity.vo.digitalizeComponentNote.DigitalizeComponentNoteVo;
import com.jeesite.modules.digitalize.util.response.ResultVo;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 记录 视点 标签 批注信息(DigitalizeComponentNote)表服务实现类
 *
 * @author makejava
 * @since 2023-11-18 14:15:37
 */
@Service
public class DigitalizeComponentNoteService extends ServiceImpl<DigitalizeComponentNoteDao, DigitalizeComponentNote> {
    @Resource
    private UserService userService;

    /**
     * 列表
     *
     * @param queryDto query
     * @return ResultVo
     */
    public ResultVo<List<DigitalizeComponentNoteVo>> selectAll(DigitalizeComponentNoteQueryDto queryDto) {
        List<DigitalizeComponentNote> list = this.lambdaQuery()
                .eq(DigitalizeComponentNote::getNoteType, queryDto.getNoteType())
                .eq(DigitalizeComponentNote::getComponentId, queryDto.getComponentId())
                .eq(StrUtil.isNotBlank(queryDto.getVersionId()), DigitalizeComponentNote::getVersionId, queryDto.getVersionId())
                .eq(StrUtil.isNotBlank(queryDto.getCreateBy()), DigitalizeComponentNote::getCreateBy, queryDto.getCreateBy())
                .eq(StrUtil.isNotBlank(queryDto.getProjectId()), DigitalizeComponentNote::getProjectId, queryDto.getProjectId())
                .orderByDesc(DigitalizeComponentNote::getId)
                .list();
        List<User> userList = userService.findList(new User());
        Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getUserName));
        List<DigitalizeComponentNoteVo> noteVos = list.stream().map(e ->
                BeanUtil.copyProperties(e, DigitalizeComponentNoteVo.class).setCreateByName(userMap.getOrDefault(e.getCreateBy(), null))
        ).collect(Collectors.toList());
        return ResultVo.success(noteVos);
    }

    /**
     * 新增
     *
     * @param dto dto
     * @return ResultVo
     */
    public ResultVo<Object> add(DigitalizeComponentNoteDto dto) {
        if(dto.getFileType() == 0 && (StringUtils.isEmpty(dto.getVersionId()) || StringUtils.isEmpty(dto.getVersionName()))){
            return ResultVo.failure("模型的版本信息不能为空！");
        }
        DigitalizeComponentNote note = BeanUtil.copyProperties(dto, DigitalizeComponentNote.class);
        note.setDelStatus(Constants.DEL_STATUA_0);
        boolean save = this.save(note);
        return save ? ResultVo.success() : ResultVo.failure();
    }

    /**
     * 编辑
     *
     * @param dto dto
     * @return ResultVo
     */
    public ResultVo<Object> edit(DigitalizeComponentNoteEditDto dto) {
        this.lambdaUpdate().set(DigitalizeComponentNote::getNoteContent, dto.getNoteContent())
                .eq(DigitalizeComponentNote::getId, dto.getId()).update();
        return ResultVo.success();
    }

    public ResultVo<DigitalizeComponentNoteVo> selectOne(Long id) {
        DigitalizeComponentNote note = this.getById(id);
        User user = userService.get(note.getCreateBy());
        DigitalizeComponentNoteVo digitalizeComponentNoteVo = BeanUtil.copyProperties(note, DigitalizeComponentNoteVo.class).setCreateByName(user.getUserName());
        return ResultVo.success(digitalizeComponentNoteVo);
    }

    /**
     * 版本选择框
     * @param queryDto queryDto
     * @return ResultVo
     */
    public ResultVo<List<OptionSelectVo>> versionOptionSelect(DigitalizeComponentNoteQueryDto queryDto) {
        List<DigitalizeComponentNote> list = this.lambdaQuery()
                .eq(DigitalizeComponentNote::getNoteType, queryDto.getNoteType())
                .eq(DigitalizeComponentNote::getComponentId, queryDto.getComponentId())
                .eq(DigitalizeComponentNote::getProjectId, queryDto.getProjectId())
                .list();
        if(CollUtil.isEmpty(list)){
            return ResultVo.success();
        }
        //去重整合
        return ResultVo.success(list.stream().map(e ->
                new OptionSelectVo(e.getVersionId(), e.getVersionName())
        ).distinct().collect(Collectors.toList()));
    }

    /**
     * 创建人选择框
     * @param queryDto queryDto
     * @return ResultVo
     */
    public ResultVo<List<OptionSelectVo>> createOptionSelect(DigitalizeComponentNoteQueryDto queryDto) {
        List<DigitalizeComponentNote> list = this.lambdaQuery()
                .eq(DigitalizeComponentNote::getNoteType, queryDto.getNoteType())
                .eq(DigitalizeComponentNote::getComponentId, queryDto.getComponentId())
                .eq(DigitalizeComponentNote::getProjectId, queryDto.getProjectId())
                .list();
        if(CollUtil.isEmpty(list)){
            return ResultVo.success();
        }
        List<String> userIdList = list.stream().map(DigitalizeComponentNote::getCreateBy).distinct().collect(Collectors.toList());
        List<User> userList = userService.findList(new User());
        Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getUserName));
        List<OptionSelectVo> collect = userIdList.stream().map(e -> new OptionSelectVo(e, userMap.getOrDefault(e, ""))).collect(Collectors.toList());
        return ResultVo.success(collect);
    }
}

